
import {mapState,mapMutations} from 'vuex'
import {imgBaseUrl} from 'src/config/env'
import headTop from 'src/components/header/head'
import footGuide from 'src/components/footer/footGuide'
import shopList from 'src/components/common/shoplist'
import {msiteAddress, msiteFoodTypes, cityGuess} from 'src/service/getData'
import 'src/plugins/swiper.min.js'
import 'src/style/swiper.min.css'
import {setStore,getStore} from '../../config/mUtils'


export default {
  data(){
    return {
      cityName:'',
      msiteTitle: '请选择地址...', // msite页面头部标题
      foodTypes: [], // 食品分类列表
      hasGetData: false, //是否已经获取地理位置数据，成功之后再获取商铺列表信息
      imgBaseUrl, //图片域名地址
    }
  },
  // async beforeMount(){
  //   document.addEventListener( "plusready", function(){
  //     // 监听点击消息事件
  //     plus.push.addEventListener( "click", function( msg ) {
  //       /*
  //          */
  //       alert(msg);
  //
  //     }, false );
  //     // 监听在线消息事件
  //     plus.push.addEventListener( "receive", function( msg ) {
  //       alert(msg);
  //     }, false );
  //   }, false );
  //   if (!this.searchAddress) {
  //     let _this=this;
  //     document.addEventListener('plusready', function () {
  //       plus.geolocation.getCurrentPosition(function(p){
  //         _this.SAVE_GEOHASH(p.coords.latitude + ',' + p.coords.longitude);
  //         _this.SAVE_CITY(p.address.city);
  //         if(p.address.poiName){
  //           _this.msiteTitle =p.address.district+p.address.poiName;
  //         }else{
  //           _this.msiteTitle =p.address.district;
  //         }
  //         _this.cityName=p.address.city;
  //         let searchAddress={
  //           geohash:p.coords.latitude + ',' + p.coords.longitude,
  //           title:_this.cityName,
  //           address:p.address.district+p.address.poiName
  //         }
  //         _this.CHOOSE_SEARCH_ADDRESS(searchAddress);
  //         // 记录当前经度纬度
  //         _this.hasGetData = true;
  //       }, function(e){
  //         alert('Geolocation error: ' + e.message);
  //       },{enableHighAccuracy:true,provider:'baidu'});
  //     }, false);
  //   }else{
  //     this.geohash = this.searchAddress.geohash;
  //     this.cityName=this.searchAddress.title;
  //     this.msiteTitle = this.searchAddress.address;
  //     this.hasGetData = true;
  //   }
  // },
   beforeMount(){
    if (!this.searchAddress) {
    let _this=this;
      !function(mapInit) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://api.map.baidu.com/api?v=2.0&ak=dSg3Ui73VByiwZF257tqK8l7&callback=mapinit";
        document.body.appendChild(script);
        window['mapinit'] = function() {
          mapInit();
        };
        document.head.appendChild(script);
      }(function() {
        var geolocation = new BMap.Geolocation();
        var gc = new BMap.Geocoder();
        geolocation.getCurrentPosition( function(r) {   //定位结果对象会传递给r变量
            if(this.getStatus() == BMAP_STATUS_SUCCESS)
            {  //通过Geolocation类的getStatus()可以判断是否成功定位。
              var pt = r.point;
              // _this.geohash=r.point.lat + ',' + r.point.lng;
              gc.getLocation(pt, function(rs){
                var addComp = rs.addressComponents;
                //保存geohash 到vuex
                _this.SAVE_GEOHASH(r.point.lat + ',' + r.point.lng);
                _this.SAVE_CITY(addComp.city);
                _this.msiteTitle = addComp.city + addComp.district + addComp.street ;
                _this.cityName=addComp.city;
                let searchAddress={
                  geohash:r.point.lat + ',' + r.point.lng,
                  title:addComp.city,
                  address:addComp.city + addComp.district + addComp.street
                }
                _this.CHOOSE_SEARCH_ADDRESS(searchAddress);
                // 记录当前经度纬度
                _this.hasGetData = true;
              });
            }
            else
            {
              switch( this.getStatus() )
              {
                case 2:
                  alert( '位置结果未知 获取位置失败.' );
                  break;
                case 3:
                  alert( '导航结果未知 获取位置失败..' );
                  break;
                case 4:
                  alert( '非法密钥 获取位置失败.' );
                  break;
                case 5:
                  alert( '对不起,非法请求位置  获取位置失败.' );
                  break;
                case 6:
                  alert( '对不起,当前 没有权限 获取位置失败.' );
                  break;
                case 7:
                  alert( '对不起,服务不可用 获取位置失败.' );
                  break;
                case 8:
                  alert( '对不起,请求超时 获取位置失败.' );
                  break;
              }
            }
          },
          {enableHighAccuracy: true}
        )
      });
  }else{
    this.geohash = this.searchAddress.geohash;
    this.cityName=this.searchAddress.title;
    this.msiteTitle = this.searchAddress.address;
    this.hasGetData = true;
  }
},
mounted(){
  //获取导航食品类型列表
  msiteFoodTypes().then(res => {
    console.log('res',res)
  let resLength = res.length;
  console.log(resLength)
  let resArr = [...res]; // 返回一个新的数组
  let foodArr = [];
  for (let i = 0, j = 0; i < resLength; i += 8, j++) {
    foodArr[j] = resArr.splice(0, 8);
  }
  this.foodTypes = foodArr;
}).then(() => {
    //初始化swiper
    new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      loop: true
    });
})
},
components: {
  headTop,
    shopList,
    footGuide,
},
computed: {
  ...mapState(["geohash","searchAddress"])
},
methods: {
...mapMutations([
    'RECORD_ADDRESS', 'SAVE_GEOHASH','SAVE_CITY','CHOOSE_SEARCH_ADDRESS'
  ]),
    // 解码url地址，求去restaurant_category_id值
    getCategoryId(url){
    let urlData = decodeURIComponent(url.split('=')[1].replace('&target_name',''));
    if (/restaurant_category_id/gi.test(urlData)) {
      return JSON.parse(urlData).restaurant_category_id.id
    }else{
      return ''
    }
  },
},
watch: {

}
}
